package com.zhhs.galaxyott.activity.mainActivity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;

import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.BaseActivity;
import com.zhhs.galaxyott.adapters.HomeMovieListAdapter;
import com.zhhs.galaxyott.customview.myListView;
import com.zhhs.galaxyott.sharepreference.SharedPrefHelper;
import com.zhhs.galaxyott.utils.Logs;

public class MainActivity extends BaseActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    public static int CurrentListPostion = 0;

    // 此版本没有用Listview来设置电影列表，如果要实现效果图中的效果则会出现反应迟钝的现象
    myListView mListView;
    HomeMovieListAdapter mAdapter;

    List<String> listData;
    private String urlString;

    private MainHolder mHolder;
    private MainPresenter mPresenter;

    private SharedPrefHelper sp;

    @Override
    protected void dealLogicBeforeInitView() {
        super.dealLogicBeforeInitView();
        sp = SharedPrefHelper.getInstance(this);
        getSmartCard();
        getStbId();

    }

    @Override
    protected void onCreateView() {
        setContentView(R.layout.activity_main);

        mPresenter = new MainPresenter(this);
        mHolder = new MainHolder(this);
        mPresenter.attachView(mHolder);

        mPresenter.loadisVipData();
        mPresenter.loadData();

        // 测试屏幕的宽度，单位是dip
        // Configuration config = getResources().getConfiguration();
        // int smallestScreenWidth = config.smallestScreenWidthDp;
        // Logs.logD("DEBUG_LC", "smallest width : " + smallestScreenWidth);

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Logs.logD("DEBUG_LC", "THIS IS -->onRestart()");
        // initPreview(urlString, "1");
    }

    @Override
    protected void onResume() {
        super.onResume();
        // initPreview(urlString);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        mHolder.loadImg.setVisibility(View.GONE);
        // mHolder.player.stopPlayer();
        Logs.logD("DEBUG_LC", "THIS IS -->onStop()");
        super.onStop();
    }

    OnFocusChangeListener mOnFocusChangeListener = new OnFocusChangeListener() {

        @SuppressLint("NewApi")
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                Logs.logD("DEBUG_L", "THIS IS onFocusChange--->1");
                v.setBackgroundResource(R.drawable.border_select);

            } else {
                v.setBackgroundResource(R.drawable.border_normal);
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            // 此条件可以保证moviesLayout回到上次选中的条目
            if (mHolder.moviesLayout.findFocus() != null) {
                // 此处应该用MainPresenter衔接，不过由于没有什么特殊的操作，所以不用
                mHolder.setNextFocus(mHolder.moviesLayout.findFocus());

            }

            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (mHolder.moviesLayout.findFocus() != null) {
                mHolder.setNextFocus(mHolder.moviesLayout.findFocus());

            }

            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (mHolder.moviesLayout.findFocus() != null) {
                mHolder.setMovieSelect("1");
                if (mHolder.moviesLayout.findFocus() == mHolder.getTextView(MainHolder.listData.size() - 1)) {
                    mHolder.setNextFocus(mHolder.getTextView(MainHolder.listData.size() - 1));
                    mHolder.getTextView(MainHolder.listData.size() - 1).setNextFocusDownId(R.id.homepage_moremovie);
                }
            }

            break;

        case KeyEvent.KEYCODE_DPAD_UP:
            if (mHolder.moviesLayout.findFocus() != null) {
                mHolder.setMovieSelect("-1");
            }

            break;

        default:
            break;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void getStbId() {
        Class<?> clazz = null;
        try {
            clazz = Class.forName("android.os.SystemProperties");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Method get = null;
        try {
            get = clazz.getMethod("get", String.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        try {
            String stb_id = (String) get.invoke(null, "ro.di.stb_id");
            sp.setStbid(stb_id);

            Logs.logD("DEBUG_LC", "this is stb_id-->" + sp.getStbid());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    private void getSmartCard() {
        BroadcastReceiver Receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                // 卡号
                String smartcard = intent.getExtras().getString("smartcard");
                // epg地址
                String epg = intent.getExtras().getString("EPG");
                // 浏览器Cookies
                String cookies = intent.getExtras().getString("CookieString");

                sp.setsmartcard(smartcard);
                sp.setepgUrl(epg);
                sp.setCookies(cookies);
                Logs.logD("DEBUG_LC", "this is smartcard-->" + sp.getsmartcard() + sp.getepgUrl() + sp.getCookies());

            }
        };
        this.registerReceiver(Receiver, new IntentFilter("com.ipanel.join.cq.vodauth.EPG_URL"));

    }
}
